package com.example.administrator.xtu_community.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.example.administrator.xtu_community.Adapter.HomeRecyclerViewAdapter;
import com.example.administrator.xtu_community.R;
import com.example.administrator.xtu_community.bean.Note;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

public class SearchAct extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
        recyclerView.setLayoutManager(layoutManager);
        HomeRecyclerViewAdapter adapter = new HomeRecyclerViewAdapter();
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.set(1, 1, 1, 1);
            }
        });
        EditText editText = findViewById(R.id.search_btn);
        View toSearch = findViewById(R.id.to_search);
        toSearch.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onClick(View v) {
                String txt = editText.getText().toString();
                List<Note> list = DataSupport.findAll(Note.class);
                List<Note> list1 = new ArrayList<>();
                for(int i = 0 ; i < list.size() ; i ++) {
                    if(list.get(i).getTitle().contains(txt)) {
                        list1.add(list.get(i));
                    }
                }
                adapter.setmVideoList(list1);
                adapter.notifyDataSetChanged();
            }
        });
    }
}